package com.zhr.tea.view.adapter;


import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhr.tea.R;
import com.zhr.tea.entity.FenLeiProduct;

import java.util.List;

/**
 * Created by zhang on 2016/11/2.
 */

public class FenLeiProductAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener{


    @Override
    public void onClick(View view) {
        if (recyClickListener!=null){
            recyClickListener.onItemClick((Integer) view.getTag());

        }
    }
    private XRecyClickListener recyClickListener = null;

    public interface XRecyClickListener{
        void onItemClick(int title);
    }


    public void setOnItemClickListener(XRecyClickListener r){
        this.recyClickListener=r;
    }


    private List<FenLeiProduct.ContentBean.GoodsListBean> data;
    private Context context;

    public FenLeiProductAdapter(List<FenLeiProduct.ContentBean.GoodsListBean> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder vh = null;
        View view = LayoutInflater.from(context).inflate(R.layout.item_car_foot, null);
        vh= new ItemViewHolder(view);
        view.setOnClickListener(this);
        return vh;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ItemViewHolder &&  data.size()!=0){
            Glide.with(context).load(data.get(position).getGoodsImgUrl()).into(((ItemViewHolder) holder).carFootIv);
            ((ItemViewHolder) holder).carFootTvTitle.setText(data.get(position).getGoodsName());
            ((ItemViewHolder) holder).carFootTvPrice.setText("￥" + data.get(position).getGoodsPrice());
            ((ItemViewHolder) holder).carFootTvCount.setText("已售" + data.get(position).getSaleCount() + "份");
            holder.itemView.setTag(data.get(position).getGoodsId());
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }



    public class ItemViewHolder extends RecyclerView.ViewHolder {

        public ImageView carFootIv;
        public TextView carFootTvTitle;
        public TextView carFootTvPrice;
        public TextView carFootTvCount;

        public ItemViewHolder(View itemView) {
            super(itemView);
            this.carFootIv = (ImageView) itemView.findViewById(R.id.car_foot_iv);
            this.carFootTvTitle = (TextView) itemView.findViewById(R.id.car_foot_tv_title);
            this.carFootTvPrice = (TextView) itemView.findViewById(R.id.car_foot_tv_price);
            this.carFootTvCount = (TextView) itemView.findViewById(R.id.car_foot_tv_count);
        }
    }


}
